﻿@model WayCloud.ViewModel.Apply.ApplyCondition
@{
    ViewBag.Title = "申请列表";
    var isAdmin = User.IsInRole("admin");
}
<script type="text/javascript">

    function createViewLink(cellvalue, options, rowData) {
        return "<a href='##' name='viewLink' rowIndex='" + options.rowId + "' style='color:#0045FF'>" + cellvalue + "</a>";
    }

    function createApprovalLink(cellvalue, options, rowData) {
        if (rowData.Status === "待审批") {
            return "<a href='##' name='approvalLink' style='color:#f60' rowIndex='" + options.rowId + "'>审批</a>";
        }
        return "";
    }

    function createEditLink(cellvalue, options, rowData) {
        if (rowData.Status === "暂存") {
            return "<a href='##' name='editLink'  style='color:#f60' rowIndex='" + options.rowId + "' >修改</a>";
        }
        return "";
    }

    function serializeCondition() {
        return $("#divSearch").children().serialize();
    }

    function getRowData(rowIndex) {
        return $("#list").getRowData(rowIndex);
    }

    function createModelDialog(title, width, height) {
        $("#editDiv").dialog({
            modal: true,
            width: width,
            title: title,
            draggable: false,
            resizable: false,
            open: function (event) {
                $("#editDiv").data("dialog", event.target);
            },
            beforeClose: function () {
                $("#list").trigger("reloadGrid");
                $("#editDiv").dialog("destroy");
            }
        });
    }

    function gridComplete() {
        //查看链接
        $("a[name='viewLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            $("#editDiv").load("/apply/details/" + rowData.Id, function () {
                createModelDialog("申请详情：        " + rowData.Title, 800);
            });
        });

        //审批链接
        $("a[name='approvalLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            $("#editDiv").load('/apply/find/' + rowData.Id, function () {
                createModelDialog("审批申请:   " + rowData.Title, 800);
            });
        });

        //修改链接
        $("a[name='editLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            $("#editDiv").load('/apply/edit/' + rowData.Id, function () {
                createModelDialog("修改申请:   " + rowData.Title, 800);
            });
        });

    }

    $().ready(function () {
        var isAdmin = '@isAdmin' == 'True';
        $.ajaxSetup({ cache: false });
        $("#list").jqGrid({
            url: '/apply/list/',
            datatype: "json",
            colNames: ['编号', '标题', 'CPU(Core)', '内存(MB)', '存储(GB)', '备注', '状态', '申请用户', '修改', '审批'],
            colModel: [
                { name: 'Id', index: 'Id', width: 200, align: 'center', hidden: true },
                { name: 'Title', index: 'Title', width: 150, align: 'left', formatter: createViewLink },
                { name: 'CPUCore', index: 'CPUCore', width: 70, align: 'right' },
                { name: 'MemroySize', index: 'MemroySize', width: 70, align: "right" },
                { name: 'DiskSize', index: 'DiskSize', width: 70, align: 'right' },
                { name: 'Comment', index: 'Comment', width: 200, align: "left", hidden: true },
                { name: 'Status', index: 'Status', width: 100, align: "center" },
                { name: 'UserName', index: 'UserName', width: 200, align: "left" },
                { name: 'Modify', index: 'Id', width: 80, align: "center", sortable: false, formatter: createEditLink },
                { name: 'Approval', index: 'Id', width: 80, align: "center", sortable: false, formatter: createApprovalLink, hidden: !isAdmin }
            ],
            autowidth: true,
            hidegrid: false,
            height: "auto",
            rowNum: 10,
            pager: '#pager',
            emptyrecords: "没有满足条件的记录",
            viewrecords: true,
            caption: "申请列表",
            multiselect: false,
            jsonReader: {
                repeatitems: false
            },
            gridComplete: gridComplete
        }).navGrid('#pager', { edit: false, add: false, del: false, view: false, search: false });

        $("#btnSearch").button().click(function () {
            var url = '/apply/list?' + serializeCondition();
            $("#list").jqGrid('setGridParam', { url: url }).trigger("reloadGrid");
        });

        $("#btnAdd").button().click(function () {
            $("#editDiv").load('/apply/find/', function () {
                createModelDialog("新建申请", 800);
            });
        });

    });
</script>
<div>
    <div style="margin: 5px; text-align: left;" id="divSearch">
        @Html.LabelFor(p => p.Title):@Html.EditorFor(p => p.Title)
        @Html.LabelFor(p => p.ApplyUserName):@Html.EditorFor(p => p.ApplyUserName)
        <input type="button" id="btnSearch" value="查  询" />
        <input type="button" id="btnAdd" value="新 增" />
    </div>
    <table id="list" width="100%">
    </table>
    <div id="pager">
    </div>
</div>
<div id="editDiv">
</div>
